package com.zy.explore.search.interfaces.facade.v1;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zy.explore.search.application.service.SearchService;
import com.zy.explore.search.infrastructure.constant.Router;
import com.zy.explore.search.interfaces.frm.ArticleQueryFrm;
import com.zy.explore.search.interfaces.frm.ArticleSaveFrm;
import com.zy.explore.search.interfaces.frm.CommonIdFrm;
import com.zy.explore.search.interfaces.frm.CommonIdsFrm;
import com.zy.explore.search.interfaces.vo.ArticleVO;
import com.zy.explore.search.interfaces.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"search门面"})
@RestController
@RequestMapping("/search")
public class SearchFacade {

    @Resource
    private SearchService searchService;

    /**
     * 新增数据
     *
     * @param frm 待新增的Frm对象
     * @return 成功返回新增后的VO对象，失败返回null
     */
    @ApiOperationSupport(order = 10, author = "🇨🇳中国印🇨🇳", ignoreParameters = {"id", "keywords", "createTime", "updateTime"})
    @ApiOperation("①新增数据")
    @PostMapping(Router.INSERT)
    public ArticleVO insert(@RequestBody @Validated ArticleSaveFrm frm){
        return searchService.insert(frm);
    }

    /**
     * 删除数据
     *
     * @param frm 主键Frm对象
     * @return 1:删除成功 其他:删除失败
     */
    @ApiOperationSupport(order = 20, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("②删除数据")
    @PostMapping(Router.DELETE)
    public Integer delete(@RequestBody @Validated CommonIdFrm frm) {
        return searchService.deleteById(frm);
    }

    /**
     * 更新数据
     *
     * @param frm 待更新的Frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    @ApiOperationSupport(order = 30, author = "🇨🇳中国印🇨🇳", ignoreParameters = {"createTime", "updateTime"})
    @ApiOperation("③更新数据")
    @PostMapping(Router.UPDATE)
    public ArticleVO update(@RequestBody @Validated ArticleSaveFrm frm) {
        return searchService.update(frm);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，查询不到返回null
     */
    @ApiOperationSupport(order = 40, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("④根据主键查询数据")
    @PostMapping(Router.FIND_BY_ID)
    public ArticleVO findById(@RequestBody @Validated CommonIdFrm frm) {
        return searchService.findById(frm.getId());
    }

    /**
     * 通过主键集合查询多条数据
     *
     * @param frm 主键集合Frm对象
     * @return 成功返回对应ID的VO对象，查询不到返回null
     */
    @ApiOperationSupport(order = 50, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑤根据主键集批量查询数据")
    @PostMapping(Router.FIND_BY_IDS)
    public List<ArticleVO> findByIds(@RequestBody @Validated CommonIdsFrm frm) {
        return searchService.findByIds(frm);
    }

    /**
     * 根据条件获取数据集
     *
     * @param frm 查询条件对象
     * @return 满足条件的数据集合,非null
     */
    @ApiOperationSupport(order = 60, author = "🇨🇳中国印🇨🇳", ignoreParameters = {"pageNum", "pageSize"})
    @ApiOperation("⑥根据条件查询数据集")
    @PostMapping(Router.FIND_LIST)
    public List<ArticleVO> findList(@RequestBody @Validated ArticleQueryFrm frm) {
        return searchService.findList(frm);
    }

    /**
     * 根据条件查询数据分页
     *
     * @param frm 查询条件参数
     * @return 成功返回满足条件的分页，非null
     */
    @ApiOperationSupport(order = 70, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑦根据条件查询数据分页")
    @PostMapping(Router.FIND_PAGE)
    public PageVO<ArticleVO> findPage(@RequestBody @Validated ArticleQueryFrm frm) {
        return searchService.findPage(frm);
    }

    /**
     * 删除所有数据
     *
     * @return 1:删除成功 其他:删除失败
     */
    @ApiOperationSupport(order = 80, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑧删除所有数据")
    @PostMapping("/deleteAll")
    public Integer deleteAll() {
        return searchService.deleteAll();
    }


}
